package android.itv.library;

import java.util.Calendar;

import android.itv.model.BroadcastSchedule;
import android.util.Log;

public class Utils
{
    public static int getProgramePlayedNow(BroadcastSchedule bs)
    {
	int position=0,hour,minute, tmphour,tmpminute;
	Calendar ca=Calendar.getInstance();
	hour=ca.get(Calendar.HOUR_OF_DAY);
	minute=ca.get(Calendar.MINUTE);
	Calendar c=Calendar.getInstance();
	
	
	for(int i=0;i<bs.getListprograme().size();i++)
	{
	    String t=bs.getListprograme().get(i).getTime();
	    tmphour=getHour(t);
	    tmpminute=getMinute(t);
	    c.set(Calendar.HOUR_OF_DAY, tmphour);
	    c.set(Calendar.MINUTE, tmpminute);  
	    
	    if(c.compareTo(ca)==-1)
	    {
		position=i;
	    }
	
	}
	return position;
    }
    
    public static int getHour(String time)
    {
	
	int hour=0;
	for(int j=0;j<time.length();j++)
	{
	    if(time.charAt(j)=='h'||time.charAt(j)=='H')
	    {
		hour=Integer.parseInt(time.substring(0, j));
	    }
	}
	
	return hour;
    }
    
    public static int getMinute(String time)
    {
	int minute=0;
	for(int j=0;j<time.length();j++)
	{
	    if(time.charAt(j)=='h'||time.charAt(j)=='H')
	    {
		minute=Integer.parseInt(time.substring(j+1, time.length()));
	    }
	}
	
	return minute;
    }
    
    public static String getDate(String date,String month,String year)
    {
	if(date.length()<2) date="0"+date;
	if(month.length()<2)month="0"+month;
	String d=date+"/"+month+"/"+year;
	Log.d("DDDD",d);
	return d;
    }
}
